<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>class与async的结合</title>
</head>
<body>
    <script>
        function ajax(url){
            return new Promise((resolve,reject) => {
                if(!/^http/.test(url)){
                    throw new ParamError("请求地址格式错误");
                }

                let xhr = new XMLHttpRequest();
                xhr.open("GET",url);
                xhr.send();
                xhr.onload = function(){
                    if(this.status == 200){
                        resolve(JSON.parse(this.response));
                    }else if(this.status == 404){
                        reject(new HttpError("请求参数错误"));
                    }else{
                        reject("加载失败");
                    }
                };
                xhr.onerror = function (){
                    reject(this);
                }
            })
        }

        class Type{
            constructor(type){
                this.type = type
            }

            then(resolve,reject){
                let jiaDianList =  ajax(`http://localhost:8008/index.php/api/home/getJiaDianList?type=${this.type}`);
                resolve(jiaDianList);
            }
        }

        async function hd (){
            let daJiaDian = await new Type(1);
            console.log(daJiaDian);
        }

        hd();
    </script>
</body>
</html>